package test.math.lesson3;

import java.util.Arrays;

public class Lesson3_3 {

    public  static boolean search(String[] dicionary, String worldToFind) {

        if (dicionary == null) {
            return false;
        }

        if (dicionary.length == 0) {
            return false;
        }

        int left = 0, right = dicionary.length - 1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (dicionary[middle].equals(worldToFind)) {
                return true;
            } else {
                if (dicionary[middle].compareTo(worldToFind) > 0) {
                    right = middle - 1;
                } else {
                    left = middle - 1;
                }
            }
        }

        return false;
    }


    public static void main(String[] args) {
        String[] dict = {"i", "am", "one", "of", "the", "authors", "geekbang"};
        Arrays.sort(dict);
        String wordToFind = "i";
        boolean found = search(dict, wordToFind);
        if (found) {
            System.out.println(String.format("找到了单词 %s", wordToFind));
        } else {
            System.out.println(String.format("未能找到担心 %s", wordToFind));
        }
    }


}
